package org.colafries.admin.entity.vo;

import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import org.colafries.common.enums.ColumnDataTypeEnum;
import org.colafries.common.enums.Flag;
import org.colafries.common.utils.HashUtil;

import java.util.List;

/**
 * @author colafries
 * @version v1.0
 * @ClassName ModelReqVO
 * @Description
 * @date Created in 2024/3/14
 **/
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
public class DataModelReqVO extends ColafriesBaseVO {
    @ApiModelProperty(value = "模型id")
    private Integer modelId;
    @ApiModelProperty(value = "模型code")
    private String code;
    @ApiModelProperty(value = "模型名称")
    private String modelName;
    @ApiModelProperty(value = "自定义查询SQL")
    private String customSql;
    @ApiModelProperty(value = "模型标签")
    private String modelLabel;
    @ApiModelProperty(value = "模型描述")
    private String modelDesc;
    @ApiModelProperty(value = "修改人")
    private String updateUser;
    @ApiModelProperty(value = "数据源id")
    private Integer databaseId;
    @ApiModelProperty(value = "数据模型列配置")
    private DataModelConfigDTO dataModelConfig;
    @ApiModelProperty(value = "查询用户id")
    private Integer queryUserId;
//    @ApiModelProperty(value = "数据模型类型(SQL模型、指标模型)", dataType = "string", example = "SQL_DATA_MODEL  INDEX_DATA_MODEL")
//    private DataModelTypeEnum dataModelType;
//    @ApiModelProperty(value = "指标查询参数")
//    private IndexQueryReqVO indexQueryParam;
    @ApiModelProperty(value = "模型idList")
    private List<Integer> ids;

    public String genCustomTableHash() {
        return HashUtil.genCustomTableHash(customSql);
    }

    /**
     * 数据模型列配置
     */
    @Data
    public static class DataModelConfigDTO {
        /**
         * 维度
         */
        private List<Dimension> dimensions;
        /**
         * 度量
         */
        private List<Measure> measures;
        /**
         * 数据脱敏
         */
        private List<CustomQueryReqVO.Fuzzy> fuzzyList;
    }

    /**
     * 维度
     */
    @Data
    public static class Dimension {
        @ApiModelProperty(value = "配置列id")
        private Integer id;
        @ApiModelProperty(value = "自定义表id")
        private Integer tableId;
        @ApiModelProperty(value = "自定义表code")
        private String tableCode;
        @ApiModelProperty(value = "字段hash值")
        private String fieldHash;
        @ApiModelProperty(value = "物理字段名")
        private String field;
        @ApiModelProperty(value = "列别名")
        private String alias;
        @ApiModelProperty(value = "计算表达式")
        private String expression;
        @ApiModelProperty(value = "数据类型 见数据类型字典")
        private ColumnDataTypeEnum dataType;
        @ApiModelProperty(value = "筛选项标识 1 是 0 否")
        private Flag filterFlag;
        @ApiModelProperty(value = "是否计算度量", dataType = "Boolean", example = "true/false")
        private Flag calculated;

        public String genFieldHash() {
            return HashUtil.genFieldHash(field);
        }
    }

    /**
     * 度量
     */
    @Data
    public static class Measure {
        @ApiModelProperty(value = "配置列id")
        private Integer id;
        @ApiModelProperty(value = "自定义表id")
        private Integer tableId;
        @ApiModelProperty(value = "自定义表code")
        private String tableCode;
        @ApiModelProperty(value = "字段hash值")
        private String fieldHash;
        @ApiModelProperty(value = "物理字段名")
        private String field;
        @ApiModelProperty(value = "度量别名")
        private String alias;
        @ApiModelProperty(value = "默认聚合 例如：SUM COUNT AVG ... ...")
        private String defaultAggregator;
        @ApiModelProperty(value = "计算表达式")
        private String expression;
        @ApiModelProperty(value = "数据格式")
        private Format format;
        @ApiModelProperty(value = "数据类型 见数据类型字典")
        private ColumnDataTypeEnum dataType;
        @ApiModelProperty(value = "筛选项标识 1 是 0 否")
        private Flag filterFlag;
        @ApiModelProperty(value = "计算度量标记", dataType = "Boolean", example = "YES/NO")
        private Flag calculated;

        public String genFieldHash() {
            // 计算度量 根据表达式生成FieldHash
            // 非计算度量 根据Field生成FieldHash
            return calculated == Flag.YES ? HashUtil.genFieldHash(expression) : HashUtil.genFieldHash(field);
        }
    }

    /**
     * 数据格式
     */
    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Format {
        @ApiModelProperty(value = "数据精度")
        private Integer accuracy;
        @ApiModelProperty(value = "数据格式，例如：percent")
        private String dataFormat;
        @ApiModelProperty(value = "数据单位，例如：%")
        private String unit;
    }


}
